package com.gy.utils;

import com.gy.componit.WarMap;

/**
 * @author Dsy
 * @create 2022-02-04  23:45
 */
public class CrashUtil {

    // 球和正方体检查碰撞
    public static boolean ballSquare(double ox, double oy, double r, double x, double y, double size) {
       boolean crash = distance(ox, oy, x, y) <= r ||
                distance(ox, oy, x + size, y) <= r ||
                distance(ox, oy, x+size, y+size) <= r ||
                distance(ox, oy, x, y+size) <= r;
       if(crash) return true;
        crash = distance(ox, oy, x + size/2, y+ size/2) <= r + size/2;
        return crash;
    }

    // 球和线段（仅限横线和竖线）检查碰撞
    public static boolean ballSimpleLine(double ox, double oy, double r, double x1, double y1, double x2, double y2) {
        if(x1 == x2) {
            // 竖线
            return Math.abs(x1-r) < r && y1 <= oy-r && oy+r <= y2;
        }else if(y1 == y2) {
            // 横线
            return Math.abs(y1-r) < r && x1 <= ox-r && ox+r <= x2;
        }else {
            // 斜线（以后再写）
            return false;
        }
    }

    // 球有没出去地图边界
    public static boolean ballWarMap(double ox, double oy, double r, WarMap map) {
        int x1 = map.getX(), x2 = x1 + map.getWidth();
        int y1 = map.getY();
        int y2 = y1 + map.getHeight();
        ox += r; oy+=r;
        return x1+r >= ox || ox >= x2-r+1 || oy >= y2-r+1 || oy <= y1+r;
    }

    private static double distance(double x1, double y1, double x2, double y2) {
        return Math.sqrt((x1-x2) * (x1-x2) + (y1-y2) * (y1-y2));
    }
}
